package com.avic.map.controller;

import com.avic.map.common.constant.AppConstant;
import com.avic.map.common.constant.SystemLogConstant;
import com.avic.map.common.constant.UserConstant;
import com.avic.map.common.util.AssertUtil;
import com.avic.map.common.util.ex.BizErrorCode;
import com.avic.map.common.util.lang.BaseResponse;
import com.avic.map.common.util.lang.ServiceCallBack;
import com.avic.map.controller.info.MapAudioInfo;
import com.avic.map.controller.request.CreateMapAudioReq;
import com.avic.map.controller.request.GetMapAudioListReq;
import com.avic.map.controller.result.CreateMapAudioRes;
import com.avic.map.controller.result.GetMapAudioListRes;
import com.avic.map.dal.condition.PageQueryMapAudioCondition;
import com.avic.map.dal.model.MapAudio;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author huangsheng on 2018/12/23
 */

@Slf4j
@RestController
@RequestMapping("audio")
@Api(tags = "语音记录", description = "语音记录,增加删除查看接口")
public class MapAudioController extends AbstractFacadeImpl {

    @PostMapping("list")
    @ApiOperation(value = "分页获取语音通话记录列表")
    public GetMapAudioListRes getMapAudioList(@RequestBody GetMapAudioListReq getMapAudioListReq) {
        log.info("分页获取语音通话记录列表-getMapAudioList");
        final GetMapAudioListRes result = new GetMapAudioListRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
            }

            @Override
            public void doService() throws Exception {
                PageQueryMapAudioCondition pageQueryMapAudioCondition =
                        new PageQueryMapAudioCondition(getMapAudioListReq.getCurrentPage(), getMapAudioListReq.getPageSize());
                pageQueryMapAudioCondition.setCreateBeginTime(getMapAudioListReq.getCreateBeginTime());
                pageQueryMapAudioCondition.setCreateEndTime(getMapAudioListReq.getCreateEndTime());

                List<MapAudio> mapAudios = mapAudioService.queryAudioListRecords(pageQueryMapAudioCondition);

                List<MapAudioInfo> mapAudioInfos = new ArrayList<>();
                if (null != mapAudios) {
                    mapAudios.forEach(mapAudio -> {
                        MapAudioInfo mapAudioInfo = new MapAudioInfo();
                        mapAudioInfo.setId(mapAudio.getId());
                        mapAudioInfo.setAudioAddress(mapAudio.getAudioAddress());
                        mapAudioInfo.setCreateTime(StringUtils.isNotBlank(mapAudio.getCreateTime()) ?
                                StringUtils.substringBefore(mapAudio.getCreateTime(), AppConstant.MYBATIS_DATA_TIME_SEPARATOR) : StringUtils.EMPTY);
                        mapAudioInfos.add(mapAudioInfo);
                    });
                }
                result.setMapAudioInfos(mapAudioInfos);
                result.setTotalRecodes(mapAudioService.queryAudioListRecordsTotalCount(pageQueryMapAudioCondition));
                result.setSuccess(true);
            }
        });
    }

    @PostMapping
    @ApiOperation(value = "创建记录")
    public CreateMapAudioRes createMapAudio(@RequestBody CreateMapAudioReq createMapAudioReq) {
        final CreateMapAudioRes result = new CreateMapAudioRes();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                AssertUtil.isNotBlank(createMapAudioReq.getAudioAddress(), "回放地址不能为空");

            }

            @Override
            public void doService() throws Exception {
                MapAudio mapAudio = new MapAudio();
                mapAudio.setAudioAddress(createMapAudioReq.getAudioAddress());

                result.setCreateId(mapAudioService.insert(mapAudio));
                result.setSuccess(true);
            }
        });
    }

    @ApiOperation(value = "删除语音记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "itemId", paramType = "path", value = "资源ID", required = true)
    })
    @DeleteMapping("/{itemId}")
    public BaseResponse deleteCarResource(@PathVariable("itemId") int itemId,
                                          @RequestHeader(value = UserConstant.HEADER_CURRENT_USER_ID_KEY, required = false) String currentUserId) {
        final BaseResponse result = new BaseResponse();
        return serviceExecuteTemplate.executeService(result, new ServiceCallBack() {
            @Override
            public void check() throws Exception {
                AssertUtil.isTrue(itemId > 0, BizErrorCode.PARAM_ERROR, "数据ID大于零");

            }

            @Override
            public void doService() throws Exception {
                mapAudioService.delete(itemId);

                // 新建系统日志
                systemServiceFeignService.createSystemLog(
                        String.format(SystemLogConstant.DELETE_MAP_AUDIO, itemId), currentUserId);
                result.setSuccess(true);
            }
        });
    }
}
